package com.fgxia.driver.controller;

import com.fgxia.driver.domain.bo.DriverFreightBo;
import com.fgxia.driver.domain.vo.OrderFreightInfoVo;
import com.fgxia.driver.service.IDriverFreightService;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.FreightStatus;
import com.ruoyi.common.enums.OrderStatus;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * driver freight controller
 *
 * @author yyt
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/driver/freight")
public class DriverFreightController extends BaseController {

    private final IDriverFreightService driverFreightService;

    /**
     * 司机端，查询运单，只能根据状态查询
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo<OrderFreightInfoVo> list(@RequestParam List<String> freightStatus, PageQuery pageQuery) {
        // 只允许查询自己的运单
        TableDataInfo<OrderFreightInfoVo> pages =
            driverFreightService.selectOrderFrightPage(getUserId(), freightStatus, pageQuery);
        return pages;
    }

    /**
     * 提货
     * @return
     */
    @RepeatSubmit()
    @PutMapping("/pickup/{freightId}")
    public R<Void> pickup(@PathVariable Long freightId, @RequestBody DriverFreightBo driverFreightBo) {
        if (!driverFreightService.checkFreightStatus(freightId, OrderStatus.WAITING_FOR_PICKUP.getCode())) {
            return R.fail("运单状态不正确");
        }

        driverFreightBo.setRelevanceUserId(getUserId());
        driverFreightBo.setFreightId(freightId);
        boolean result = driverFreightService.pickup(driverFreightBo);
        return toAjax(result);
    }

    /**
     * 到货
     * @return
     */
    @RepeatSubmit()
    @PutMapping("/discharge/{freightId}")
    public R<Void> discharge(@PathVariable Long freightId, @RequestBody DriverFreightBo driverFreightBo) {
        if (!driverFreightService.checkFreightStatus(freightId, OrderStatus.DELIVERING.getCode())) {
            return R.fail("运单状态不正确");
        }

        driverFreightBo.setRelevanceUserId(getUserId());
        driverFreightBo.setFreightId(freightId);
        boolean result = driverFreightService.discharge(driverFreightBo);
        return toAjax(result);
    }

    /**
     * 打卡
     * @return
     */
    @RepeatSubmit()
    @PutMapping("/clock-in/{freightId}")
    public R<Void> clockIn(@PathVariable Long freightId, @RequestBody DriverFreightBo driverFreightBo) {
        if (!driverFreightService.checkFreightStatus(freightId, OrderStatus.DELIVERING.getCode())) {
            return R.fail("运单状态不正确");
        }

        driverFreightBo.setRelevanceUserId(getUserId());
        driverFreightBo.setFreightId(freightId);
        boolean result = driverFreightService.insertByBo(driverFreightBo);
        return toAjax(result);
    }

    /**
     * 回单
     * @return
     */
    @RepeatSubmit()
    @PutMapping("/receipt/{freightId}")
    public R<Void> receipt(@PathVariable Long freightId, @RequestBody DriverFreightBo driverFreightBo) {
        if (!driverFreightService.checkFreightStatus(freightId, OrderStatus.WAITING_FOR_RECEIPT.getCode())) {
            return R.fail("运单状态不正确");
        }

        driverFreightBo.setRelevanceUserId(getUserId());
        driverFreightBo.setFreightId(freightId);
        boolean result = driverFreightService.receipt(driverFreightBo);
        return toAjax(result);
    }
}
